<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
use Excel;

use \Lang2;
use Illuminate\Support\Facades\Log;
use \MyUtils;

class TicketController extends Controller{


    function addTicket(Request $request){
        $inputs = \MyUtils::extractInput($request);
        $header = \MyUtils::extractHeader($request);
        $flash = \MyUtils::extractFlash($request);

        $anonymous = $inputs->get("anonymous", '0');
        try{
            $error_code = 422;
            $inputs->checkFormat("content", function ($value){
                return Lang2::isNotEmpty($value);
            }, $error_code, "请填写内容");

            $inputs->checkFormat("type", function ($value){
                return Lang2::isNotEmpty($value);
            }, $error_code, "请填写类型");

            $inputs->checkFormat("department", function ($value){
                return Lang2::isNotEmpty($value);
            }, $error_code, "请填写部门");

            $inputs->checkFormat("topic", function ($value){
                return Lang2::isNotEmpty($value);
            }, $error_code, "请填写主题");

            $inputs->checkFormat("anonymous", function ($value){
                return Lang2::isNotEmpty($value);
            }, $error_code, "请填写是否匿名");

            if($anonymous == 0){
                $inputs->checkFormat("phone", function ($value){
                    return Lang2::isNotEmpty($value);
                }, $error_code, "请填写电话");

                $inputs->checkFormat("contactor", function ($value){
                    return Lang2::isNotEmpty($value);
                }, $error_code, "请填写联系人");
            }

        }catch (\CommonException $e){
            return response()->json([
                'code' => $e->getCode(),
                'message' => $e->getMessage(),
            ]);
//            return \Resp::response_json_fail($e->getCode(), $e->getMessage());
        }




        $db = \MyDB::getInstance();

        $newId = $db->insert('tickets', [
            'content' => $inputs->get("content", ''),
            'type' => $inputs->get("type", ''),
            'department' => $inputs->get("department", ''),
            'anonymous' => $inputs->get("anonymous", '0'),
            'phone' => $inputs->get("phone", ''),
            'contactor' => $inputs->get("contactor", ''),
            'topic' => $inputs->get("topic", ''),
            'reply' => "[]",
        ]);

        header('Access-Control-Allow-Origin: *');
        return response()->json([
            'code' => 0,
            'data' => ['id' => $newId],
        ]);

    }





    function listTickets(Request $request)
    {
        $currentUser = \MyUtils::getCurrentUser($request);
        $inputs = \MyUtils::extractInput($request);

        $order = $inputs->get('order', 'asc');

        $last_id = $inputs->get('last_id', 0);
        if($last_id == 0) {
            if($order != 'asc') $last_id = 999999;
        }

        $count = $inputs->get('count', 10);
        if($count <= 0) $count = 10;

        $type = $inputs->get('type', '');
        if($type == ''){
            return response()->json([
                'code' => 422,
                'message' => '必须传入type',
            ]);
        }

        $user_id = $currentUser != null ? $currentUser['id'] : 0;

        $op = '<';
        if($order == 'asc'){
            $op = '>';
        }

        $sql = "select * from tickets where type = '$type' and status = 1 and id $op $last_id order by id $order limit 0, $count";
        $tickets = \MyDB::getInstance()->findAll($sql, []);

        foreach ($tickets as &$ticket){
            $reply = $ticket['reply'];
            $replies = json_decode($reply, true);
            $ticket['reply'] = count($replies) == 0 ? '' : $replies[0]['content'];
        }

        header('Access-Control-Allow-Origin: *');
        return response()->json([
            'code' => 0,
            'data' => $tickets,
        ]);

    }






}